package com.home.work.utils;

import com.home.work.annotation.HomeWorkSpringBootApplication;
import com.home.work.annotation.HomeWorkValue;
import com.home.work.constant.Resources;
import com.home.work.exception.HasBeanException;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import static com.home.work.constant.Resources.objResources;

public class ClassUtils {
    /**
     * 类
     */
    public void classes(Class param) {

    }

    /**
     * 属性
     */
    public void field(Class param) throws HasBeanException {
        try {
            /**
             * 通过反射获取所有string类型成员变量信息，并以此读取配置文件中的值并赋值。
             */
            Field[] fields = param.getDeclaredFields();
            String objFullPath = param.getName();
            Object obj = Class.forName( objFullPath ).newInstance();

            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                if (field.isAnnotationPresent( HomeWorkValue.class )) {
                    HomeWorkValue ano = field.getAnnotation( HomeWorkValue.class );
                    String key = ano.value();
                    String value = Resources.param.get( key );
                    String name = field.getName();
                    char[] chars = name.toCharArray();
                    chars[0] -= 32;
                    String fieldName = String.valueOf( chars );
                    Method[] declaredMethods = param.getDeclaredMethods();
                    for (Method m : declaredMethods) {
                        if (m.getName().equals( "set" + fieldName )) {
                            m.invoke( obj, value );
                            break;
                        }
                    }

                }
            }
            Object o = objResources.get( objFullPath );
            if (o != null) {
                throw new HasBeanException( "该Bean已存在" );
            }
            objResources.put( objFullPath, obj );//把实例化对象放进去
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    /**
     * 方法
     */
    public void method(Class param) {
        Resources.classFullPath.forEach( a -> {
            Class<?> aClass = null;
            try {
                aClass = Class.forName( a );
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        } );
    }

    public static void run(Class param) {
        if (param.isAnnotationPresent( HomeWorkSpringBootApplication.class )) {
            System.out.println( "存在 @HomeWorkSpringBootApplication 注解" );
            // 获取全部包下的包名.类名，存储到 Resources.classFullPath
            new FileUtils().listAllFile();
            // 将YML配置文件进行解析，存储成K,V对应关系，Resources.保存在param
            new YmlUtils().getValue();

        } else {
            System.out.println( "不存在 @HomeWorkSpringBootApplication 注解" );
        }
    }


}
